--
-- Control de formato de código de usuarios y perfiles   
--

CREATE OR REPLACE FUNCTION public.tg_ambusr_ins_cod()
    RETURNS trigger
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
BEGIN
  IF NOT (SELECT length(NEW.COD) = 2 AND
  	      substring(NEW.COD,1,1) >= 'a' and substring(NEW.COD,1,1) <= 'z' and
          substring(NEW.COD,2,1) >= '0' and substring(NEW.COD,2,1) <= '9') 
  THEN
    RAISE EXCEPTION 'El codigo debe ser una letra minuscula seguida de un digito. Entre a0, z9';
  END IF;
  RETURN NEW;
END;
$BODY$;

ALTER FUNCTION public.tg_ambusr_ins_cod()
    OWNER TO abacouser;


CREATE TRIGGER ambusr_ins_cod
    BEFORE INSERT
    ON public.ambusr
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_ambusr_ins_cod();

 
CREATE TRIGGER ambusracc_ins_cod
    BEFORE INSERT
    ON public.ambusracc
    FOR EACH ROW
    EXECUTE FUNCTION public.tg_ambusr_ins_cod();   

--
SELECT aaVersion('525.02','525.01');
--

